package updater.gui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import xml.ReadXMLFile;
import xml.WriteXMLFile;

public class UpdatePreferences {

	public static final String fileName = "update_preferences.xml";
	
	public static final String rootName = "update_preferences";
	public static final String VERSION_CODE_TAG = "versionCode";
	public static final String PROGRAM_FOLDER_TAG = "programFolder";
	public static final String APPLICATION_LAUNCHER_TAG = "applicationLauncher";
	public static final String MD5_HASH_TAG = "MD5Hash";
	public static final String DOWNLOAD_URL_TAG = "downloadUrl";
	public static final String UPDATE_URL_TAG = "updateUrl";

	public static final String[] keys = { VERSION_CODE_TAG, PROGRAM_FOLDER_TAG, APPLICATION_LAUNCHER_TAG,
						MD5_HASH_TAG, DOWNLOAD_URL_TAG, UPDATE_URL_TAG};

	private Map<String, String> values = new HashMap<String, String>();

	public UpdatePreferences() {
		readPreferences();
	}

	private void readPreferences() {
		try {
			values = new ReadXMLFile().parse(new FileInputStream(new File(fileName)), keys);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			values = new HashMap<String, String>();
			for (String key : keys) {
				values.put(key, "");
			}
		}

		for (String key : values.keySet()) {
			System.out.println(key + " " + values.get(key));
		}
	}
	
	public String getValue(String key) {
		return values.get(key);
	}
	
	public void setValue(String key, String value) {
		values.put(key, value);
		savePreferences();
	}
	
	private void savePreferences() {
		new WriteXMLFile().saveToXML(fileName, rootName, values);
	}

	public static void main(String[] args) {
		UpdatePreferences pref = new UpdatePreferences();
		pref.setValue(VERSION_CODE_TAG, "1111");
		pref.savePreferences();
	}

}
